﻿Imports System.Windows.Forms
Imports System.Drawing

Public Class DiagramPanel
    Inherits Panel
    Private M_GridCol As Color = Color.Black
    Private M_GridEn As Boolean = True
    Private Sock1 As Socket
    Private Sock2 As Socket
    Private M_DrawTrace As Boolean = False
    Private M_X1 As Integer
    Private M_Y1 As Integer
    Private M_X2 As Integer
    Private M_Y2 As Integer
    Private M_Started As Boolean = False
    Private Plug1 As Plug
    Private Plug2 As Plug
    Private M_Select2 As String
    Private M_Lines As New Collection

    Private M_DiagramName As String
    Private M_Mode As EditMode = EditMode.UnLocked
    Private M_BackColor As System.Drawing.Color
    Private M_GridColor As System.Drawing.Color
    Private Debugger As New Debugger
    Private m_PlugHasControl As Boolean = False
    Public Event ModeChanged(ByVal Mode As EditMode)

    Public Enum EditMode
        Locked = 0
        UnLocked = 1
    End Enum
    Public Property DiagramName() As String
        Get
            Return M_DiagramName
        End Get
        Set(ByVal value As String)
            M_DiagramName = value
        End Set
    End Property

    Public Property Mode() As EditMode
        Get
            Return M_Mode
        End Get
        Set(ByVal value As EditMode)
            M_Mode = value
            For Each C In Me.Controls
                If C.GetType.GetInterface("I_EditMode") IsNot Nothing Then
                    Dim D As I_EditMode = C
                    D.SetEditMode(M_Mode)
                End If
            Next
            Me.Refresh()
            RaiseEvent ModeChanged(M_Mode)
        End Set
    End Property
    Public Property BackgroundColor() As System.Drawing.Color
        Get
            Return M_BackColor
        End Get
        Set(ByVal value As System.Drawing.Color)
            M_BackColor = value
            Me.BackColor = M_BackColor
            Me.Refresh()
        End Set
    End Property
    Public Property GridColor() As System.Drawing.Color
        Get
            Return M_GridColor
        End Get
        Set(ByVal value As System.Drawing.Color)
            M_GridColor = value
            Me.Refresh()
        End Set
    End Property
    Public Property ShowGrid() As Boolean
        Get
            Return M_GridEn
        End Get
        Set(ByVal value As Boolean)
            M_GridEn = value
            Me.Refresh()
        End Set
    End Property
    Private Sub workspace_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
        If M_Mode = EditMode.UnLocked Then
            If e.Button = Windows.Forms.MouseButtons.Right Then
                For Each C As Control In Me.Controls
                    For Each CC As Control In C.Controls
                        If TypeOf CC Is Socket Then
                            Dim S As Socket = CC
                            S.UnSelect()
                        End If
                    Next
                Next
                M_DrawTrace = False
                M_Started = False
                Sock1 = Nothing
                Sock2 = Nothing
                If Plug1 IsNot Nothing Then Plug1.Dispose()
                If Plug2 IsNot Nothing Then Plug2.Dispose()
                m_PlugHasControl = False
                Me.Refresh()
            End If
        End If
    End Sub
    Private Sub workspace_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
        If M_Mode = EditMode.UnLocked Then
            If M_DrawTrace Then
                If m_PlugHasControl = False Then
                    M_X2 = e.X
                    M_Y2 = e.Y
                    Me.Refresh()
                End If
            End If

            If M_Select2 > "" Then
                M_Select2 = ""
                Plug2.SetCenter(-100, -100)
                Sock2.UnSelect()
                m_PlugHasControl = False
            End If
        End If
    End Sub
    Private Sub workspace_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        If M_GridEn = True Then
            Dim G As Graphics = e.Graphics
            Dim X As Integer = 0
            Dim Y As Integer = 0
            G.Clear(Me.BackColor)
            Dim P As New Pen(M_GridColor)
            For Y = 0 To Me.Height Step 10
                G.DrawLine(P, 0, Y, Me.Width, Y)
            Next
            For X = 0 To Me.Width Step 10
                G.DrawLine(P, X, 0, X, Me.Height)
            Next
        End If
        If M_DrawTrace = True Then
            Dim G As Graphics = e.Graphics
            Dim P As New Pen(Color.Red, 8)
            G.DrawLine(P, M_X1, M_Y1, M_X2, M_Y2)
        End If
    End Sub

    Public Sub GetSockEvents(ByVal Cnt As Control)
        For Each C As Control In Cnt.Controls
            If TypeOf C Is Socket Then
                Dim S As Socket = C
                AddHandler S.SelectMe, AddressOf SelectMe
                AddHandler S.HiLite, AddressOf Sock_MouseEnter
                AddHandler S.LoLite, AddressOf Sock_MouseLeave
            End If
        Next
    End Sub
    Private Sub SelectMe(ByVal Sock As Socket)
        If M_Mode = EditMode.UnLocked Then
            If M_Started = False Then
                Sock1 = Sock
                Plug1 = New Plug
                Me.Controls.Add(Plug1)
                Plug1.SocketType = Plug.Socket_Type.Selected
                Plug1.ExitTo = Sock1.ExitTo
                Plug1.SetCenter(Sock1.CenterPoint)
                Plug1.BringToFront()
                M_X1 = Plug1.GetLinePoint.X
                M_Y1 = Plug1.GetLinePoint.Y
                M_X2 = M_X1
                M_Y2 = M_Y1
                Plug2 = New Plug

                Plug2.SetCenter(-100, -100)
                Me.Controls.Add(Plug2)
                AddHandler Plug2.MouseClick, AddressOf Plug_MouseClick
                AddHandler Plug2.UpdateWire, AddressOf Plug_UpdateWire
                Plug2.SocketType = Plug.Socket_Type.Selected

                M_Started = True
                M_DrawTrace = True
                M_Select2 = ""
                m_PlugHasControl = False
                Me.Refresh()
            Else
                Createwire()
            End If
        End If
    End Sub
    Private Sub Sock_MouseEnter(ByVal sender As Socket)
        'Debugger.Show()
        If M_Started = True Then
            If M_Select2 = "" Then
                Sock2 = sender
                Sock2.Selected = True
                Sock2.Refresh()
                Plug2.PointTo(Sock2)
                Plug2.BringToFront()
                Plug1.BringToFront()
                M_Select2 = Sock2.Name
                m_PlugHasControl = True
            End If
        End If
    End Sub
    Private Sub Sock_MouseLeave(ByVal sender As Socket)
        m_PlugHasControl = False
    End Sub
    Private Sub Plug_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        Createwire()
        m_PlugHasControl = False
    End Sub
    Private Sub Plug_UpdateWire(ByVal NewPoint As Point)
        M_X2 = Plug2.GetLinePoint.X
        M_Y2 = Plug2.GetLinePoint.Y
        Refresh()
    End Sub
    Private Sub Createwire()
        If M_Started = True Then
            Sock1.UnSelect()
            Sock2.UnSelect()

            Plug1.Dispose()
            Plug2.Dispose()
            m_PlugHasControl = False
            M_Started = False
            M_DrawTrace = False
            Me.Refresh()

            Dim X As New Wire
            X.CreateWire(Me)
            X.SetStartSocket(Sock1)
            X.SetEndSocket(Sock2)
        End If
    End Sub

End Class
